package editAir.buildEdit
{
	import _Pan3D.program.Program3DManager;
	
	import _me.Scene_data;
	
	import editAir.editContent.ModelVO;
	
	import flash.display3D.Context3D;
	import flash.display3D.Program3D;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import newPan3d.content.build.NewBuildShader;
	
	import test.zhuwei.BuildDisplay3DContainer;

	public class BuildEditLevel
	{
		private var _context3D:Context3D;
		private var _groundItem:Array;
		private var _selectItem:Array;
		private var _canMouseHitMode:Boolean=false;

		private var _display3DContainer:BuildDisplay3DContainer
		public static var buildItem:Vector.<BuildEditDisplay3DSprite>;

		public function BuildEditLevel()
		{
			_display3DContainer=new BuildDisplay3DContainer();
			_context3D=Scene_data.context3D;
			loadBuildConfigXml();


		}

		public function get canMouseHitMode():Boolean
		{
			return _canMouseHitMode;
		}

		public function set canMouseHitMode(value:Boolean):void
		{
			_canMouseHitMode = value;
		}

		public function get selectItem():Array
		{
			return _selectItem;
		}

		public function set selectItem(value:Array):void
		{
			_selectItem = value;
			
			
			for (var k:int=0; k < buildItem.length; k++)
			{
				BuildEditDisplay3DSprite(buildItem[k]).isChoose=false;	
			}
			
			if(_selectItem)
			{
				for(var i:int=0;i<_selectItem.length;i++)
				{
					for (var j:int=0; j < buildItem.length; j++)
					{
						if (buildItem[j].id == _selectItem[i])
						{
							BuildEditDisplay3DSprite(buildItem[j]).isChoose=true;
						}
					}
				}
			}
			
			
		}
		public function getBuildSpriteById(id:uint):BuildEditDisplay3DSprite
		{
			for (var j:int=0; j < buildItem.length; j++)
			{
				if (buildItem[j].id == id)
				{
					return(BuildEditDisplay3DSprite(buildItem[j]))
				}
			}
			return null;
			
		}

		private function loadBuildConfigXml():void
		{
			//加载地面XML列表

			var _loaderURL:URLRequest=new URLRequest(Scene_data.fileRoot + "build/" + "buildeditstatList.xml");
			var _scenexml:URLLoader=new URLLoader();
			_scenexml.load(_loaderURL);
			_scenexml.addEventListener(Event.COMPLETE, _loadGroundXmlOk);
			function _loadGroundXmlOk(event:Event):void
			{
				//将XML数据传到MODEL进行解析。
				preasXml(XML(_scenexml.data));

			}
		}

		private function preasXml(_xml:XML):void
		{
		

			var treeShaderProgram3d:Program3D=Program3DManager.getInstance().getProgram(NewBuildShader.NEW_BUILD_SHADER);
			_display3DContainer.setProgram3D(treeShaderProgram3d);


		}
		public function setSelectArray(arr:Array):void
		{
			_selectItem=arr;
			
		}

		public function deleBuildObj(vo:ModelVO):void
		{
			if (!vo)
			{
				return;
			}
			if (vo.filetype == 1)
			{
				return;
			}
			for (var i:int=0; i < buildItem.length; i++)
			{
				if (buildItem[i].id == vo.id)
				{
					_display3DContainer.removeChild(buildItem[i]);
					buildItem.splice(i, 1);
					return;
				}
			}


		}
		public function clearAll():void
		{
			if(buildItem){
				for(var i:int=0;i<buildItem.length;i++)
				{
					_display3DContainer.removeChild(BuildEditDisplay3DSprite(buildItem[i]));
				}
			}
			buildItem=new Vector.<BuildEditDisplay3DSprite>;
		}

		public function addBuildObj(vo:ModelVO):void
		{
			if (!vo)
			{
				return;
			}
			if (vo.filetype == 1)
			{
				return;
			}
			var display3DSprite:BuildEditDisplay3DSprite=new BuildEditDisplay3DSprite(_context3D);
			display3DSprite.url=vo.et
			display3DSprite.modeName=vo.name
			display3DSprite.x=vo.x
			display3DSprite.y=0;
			display3DSprite.z=vo.z
			display3DSprite.scale=vo.scale;
			display3DSprite.id=vo.id
			display3DSprite.rotationY=vo.angle_y;
			display3DSprite.name=String("id" + String(vo.id));
			_display3DContainer.addChild(display3DSprite);
			buildItem.push(display3DSprite);
			display3DSprite.program=Program3DManager.getInstance().getProgram(NewBuildShader.NEW_BUILD_SHADER)

		}

		public function upData():void
		{
			_display3DContainer.update();
		}

		public function get display3DContainer():BuildDisplay3DContainer
		{
			return _display3DContainer;
		}

	}
}
